﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Threading;
using System.Web.UI;

namespace NumberToTextField
{
    public class NumberToTextField : SPFieldText
    {
        
        #region Properties

        private string _quantityColumn;
        private string _localeColumn;

        public string QuantityColumn
        {
            get
            {
                if (_quantityColumn == null)
                {
                    _quantityColumn = GetFieldThreadDataValue("QuantityColumn", false);
                }
                return (!string.IsNullOrEmpty(_quantityColumn)) ? _quantityColumn : null;
            }
            set
            {
                SetFieldThreadDataValue("QuantityColumn",
                  (!string.IsNullOrEmpty(value) ? value : ""));
            }
        }


        public string LocaleColumn
        {
            get
            {
                if (_localeColumn == null)
                {
                    _localeColumn = GetFieldThreadDataValue("LocaleColumn", false);
                }
                return (!string.IsNullOrEmpty(_localeColumn)) ? _localeColumn : null;
            }
            set
            {
                SetFieldThreadDataValue("LocaleColumn",
                  (!string.IsNullOrEmpty(value) ? value : ""));
            }
        }

    
        #endregion

        public NumberToTextField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
            ShowInEditForm = false;
            ShowInNewForm = false;
      
           
        }
        
        public NumberToTextField(SPFieldCollection fields, string fieldName, string displayName)
            : base(fields, fieldName, displayName)
        {
            ShowInEditForm = false;
            ShowInNewForm = false;
   
        }
     
        public override Microsoft.SharePoint.WebControls.BaseFieldControl FieldRenderingControl
        {
            get
            {
                Microsoft.SharePoint.WebControls.BaseFieldControl fldControl = new NumberToTextFieldControl();
                fldControl.FieldName = InternalName;
           
                fldControl.ItemFieldValue = QuantityColumn;
                fldControl.ListItemFieldValue = QuantityColumn;


                //Microsoft.SharePoint.WebControls.BaseFieldControl fldControl2 = new NumberToTextFieldControl();
                //fldControl2.FieldName = InternalName;

                //fldControl2.ItemFieldValue = LocaleColumn ;
                //fldControl2.ListItemFieldValue = LocaleColumn;


                //fldControl.Controls.Add(fldControl2);

                return fldControl;
            }
        }

        #region GetFieldThreadDataValue method
        
        private string GetFieldThreadDataValue(string propertyName, bool ignoreEmptyValue)
        {
            string _d = (string)Thread.GetData(Thread.GetNamedDataSlot(propertyName));
            if (string.IsNullOrEmpty(_d) && !ignoreEmptyValue)
            {
                _d = (string)base.GetCustomProperty(propertyName);
            }
            return _d;
        }

        private void SetFieldThreadDataValue(string propertyName, string value)
        {
            
            Thread.SetData(Thread.GetNamedDataSlot(propertyName), value);
        }

        #endregion

        #region OnAdded method
        /// <summary>
        /// Fires when a new filtered lookup field is added
        /// </summary>
        /// <param name="op"></param>
        public override void OnAdded(SPAddFieldOptions op)
        {
            base.OnAdded(op);
            Update();
        }
        #endregion

        #region Update method
        /// <summary>
        /// Updates the properties of the filtered lookup field
        /// </summary>
      
        public override void Update()
        {
            UpdateFieldProperties();
            base.Update();
            CleanUpThreadData();
        }

        #endregion

        #region UpdateFieldProperties method
        
        /// <summary>
        /// Updates custom properties of the filtered lookup field
        /// </summary>
        private void UpdateFieldProperties()
        {
            string _l = GetFieldThreadDataValue("QuantityColumn", true);            
            base.SetCustomProperty("QuantityColumn", _l);

            string _2 = GetFieldThreadDataValue("LocaleColumn", true);
            base.SetCustomProperty("LocaleColumn", _2);
            
            
        }

        #endregion

        #region CleanUpThreadData method

        private void CleanUpThreadData()
        {
            Thread.FreeNamedDataSlot("QuantityColumn");
            Thread.FreeNamedDataSlot("LocaleColumn");
        }

        #endregion

    }
}
